<?php
/**
 * User: taodf
 * Date: 17-4-7
 * Time: 下午10:31
 */

namespace Zeus\Config;

use ArrayAccess;
use Noodlehaus\Config as ConfigEngine;
use Zeus\Contracts\Config\Config as ConfigInterface;

/**
 * Class Config
 * 配置管理器对象
 * @package Zeus\Config
 */
class Config implements ConfigInterface, ArrayAccess
{
    /**
     * 配置操作的引擎对象
     *
     * @var ConfigEngine $configEngine
     */
    private $configEngine;

    /**
     * Config constructor.
     * @param string $basepath 配置文件的存储路径
     */
    public function __construct($basepath)
    {
        $this->configEngine=new ConfigEngine($basepath);
    }

    /**
     * Determine if the given configuration value exists.
     *
     * @param  string $key
     * @return bool
     */
    public function has($key)
    {
        return $this->configEngine->has($key);
    }

    /**
     * Get the specified configuration value.
     *
     * @param  string $key
     * @param  mixed $default
     * @return mixed
     */
    public function get($key, $default = null)
    {
        return $this->configEngine->get($key, $default);
    }

    /**
     * Get all of the configuration items for the application.
     *
     * @return array|null
     */
    public function all()
    {
        return $this->configEngine->all();
    }

    /**
     * Set a given configuration value.
     *
     * @param  array|string $key
     * @param  mixed $value
     * @return void
     */
    public function set($key, $value = null)
    {
        $this->configEngine->set($key, $value);
    }

    /**
     * Prepend a value onto an array configuration value.
     *
     * @param  string $key
     * @param  mixed $value
     * @return void
     */
    public function prepend($key, $value)
    {
        $array=$this->configEngine->get($key);
        array_unshift($array, $value);
        $this->configEngine->set($key, $array);
    }

    /**
     * Push a value onto an array configuration value.
     *
     * @param  string $key
     * @param  mixed $value
     * @return void
     */
    public function push($key, $value)
    {
        $array=$this->configEngine->get($key);
        $array[]=$value;
        $this->configEngine->set($key, $array);
    }

    /**
     * Whether a offset exists
     * @link http://php.net/manual/en/arrayaccess.offsetexists.php
     * @param mixed $offset <p>
     * An offset to check for.
     * </p>
     * @return boolean true on success or false on failure.
     * </p>
     * <p>
     * The return value will be casted to boolean if non-boolean was returned.
     * @since 5.0.0
     */
    public function offsetExists($offset)
    {
        return $this->has($offset);
    }

    /**
     * Offset to retrieve
     * @link http://php.net/manual/en/arrayaccess.offsetget.php
     * @param mixed $offset <p>
     * The offset to retrieve.
     * </p>
     * @return mixed Can return all value types.
     * @since 5.0.0
     */
    public function offsetGet($offset)
    {
        return $this->get($offset);
    }

    /**
     * Offset to set
     * @link http://php.net/manual/en/arrayaccess.offsetset.php
     * @param mixed $offset <p>
     * The offset to assign the value to.
     * </p>
     * @param mixed $value <p>
     * The value to set.
     * </p>
     * @return void
     * @since 5.0.0
     */
    public function offsetSet($offset, $value)
    {
        $this->set($offset, $value);
    }

    /**
     * Offset to unset
     * @link http://php.net/manual/en/arrayaccess.offsetunset.php
     * @param mixed $offset <p>
     * The offset to unset.
     * </p>
     * @return void
     * @since 5.0.0
     */
    public function offsetUnset($offset)
    {
        $this->set($offset, null);
    }
}